package com.ft.oa.service.technologyService.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ft.oa.domain.dto.sys.SecurityUser;
import com.ft.oa.domain.sys.SysUser;
import com.ft.oa.domain.technology.Technology;
import com.ft.oa.domain.technology.TechnologyContributor;
import com.ft.oa.mapper.sys.SysUserMapper;
import com.ft.oa.mapper.technologyMapper.ItechnologyMapper;
import com.ft.oa.mapper.technologyMapper.ItechnologyContributorMapper;
import com.ft.oa.security.constant.SecurityConstant;
import com.ft.oa.service.technologyService.ItechnologyDocService;
import com.ft.oa.service.technologyService.ItechnologyService;
import com.ft.oa.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class TechnologyServiceImpl implements ItechnologyService {
    @Resource
    private ItechnologyMapper itechnologyMapper;
    @Resource
    private ItechnologyContributorMapper itechnologyContributorMapper;
    @Autowired
    private ItechnologyDocService itechnologyDocService;
    @Resource
    private SysUserMapper sysUserMapper;
    @Override
    public Map<String, Object> viewtechology(String rejsname, int page, int size) {
        QueryWrapper<Technology> wrapper = new QueryWrapper();
        wrapper.eq("delldust","0");
        // 获取当前用户信息
        SecurityUser currentUser = SecurityUtils.getCurrentUser();
        boolean roledesc= false;
        // 获取当前用户名称用作展示售前项目管理
        Long creator = currentUser.getId();
        //获取用户权限 如果是admin 则是管理员 可以查看到全部
        Set<String> roles = currentUser.getRoles();
        for (String role:roles) {
            if(role.equals(SecurityConstant.SUPER_ROLE)){
                roledesc=true;
            }
        }
        boolean empty = StringUtils.isEmpty(rejsname);
        if (!empty) {
            wrapper.like("jsname",rejsname);
        }
        if(roledesc) {

        }else{
            wrapper.eq("creator",creator);
        }
        Page<Technology> pageIndustrytion = new Page<>(page, size);
        Page<Technology> TechPage = itechnologyMapper.selectPage(pageIndustrytion, wrapper.orderByDesc("update_time"));
        Map<String,Object> techmap = new HashMap<>();
        long total = TechPage.getTotal();
        List<Technology> industries = TechPage.getRecords();
        List teclist = new ArrayList();
        industries.forEach(techno->{
            HashMap<String,Object> tecmap = new HashMap<>();
            Long id = techno.getId();
            tecmap.put("id",id);
            // 日期
            String newTime = techno.getUpdateTime();
            tecmap.put("newTime",newTime);
            // 技术名称
            String jsname = techno.getJsname();
            tecmap.put("jsname",jsname);
            //技术描述
            String jsdesc = techno.getJsdesc();
            tecmap.put("jsdesc",jsdesc);
            Long tecid = techno.getId();
            QueryWrapper<TechnologyContributor> contributorwrapper = new QueryWrapper<>();
            contributorwrapper.eq("technology_id",tecid);
            contributorwrapper.eq("dellpeople","0");
            Integer cyrnum = itechnologyContributorMapper.selectCount(contributorwrapper);
            tecmap.put("cyrnum",cyrnum);
            //负责人id
            Long responsibilityUserid = techno.getResponsibilityUser();
            tecmap.put("principalnameUserid",responsibilityUserid);
            SysUser sysUser = sysUserMapper.selectById(responsibilityUserid);
            // 负责人名字
            String principalname = sysUser.getNickname();
            tecmap.put("principalname",principalname);
            //解决方案 1
            tecmap.put("solution",itechnologyDocService.docvaluenum(tecid,1));
            //营销 策略
            tecmap.put("planning",itechnologyDocService.docvaluenum(tecid,2));
            //经典案例
            tecmap.put("classiccase",itechnologyDocService.docvaluenum(tecid,3));
            //行业背景
            tecmap.put("background",itechnologyDocService.docvaluenum(tecid, 4));
            teclist.add(tecmap);
        });
        techmap.put("total",total);
        techmap.put("technolist",teclist);
        return techmap;
    }

    /**
     * 添加技术领域信息
     * @param technology
     * @return
     */
    @Override
    public int addtechlogy(Technology technology) {
        // 获取当前用户信息
        SecurityUser currentUser = SecurityUtils.getCurrentUser();
        // 获取当前用户名称用作展示售前项目管理
        Long id = currentUser.getId();
        technology.setCreator(id);
        return itechnologyMapper.insert(technology);
    }
    private QueryWrapper<Technology> gainuserid(){
        // 获取当前用户信息
        SecurityUser currentUser = SecurityUtils.getCurrentUser();
        // 获取当前用户名称用作展示售前项目管理
        Long creator = currentUser.getId();
        //获取用户权限 如果是admin 则是管理员 可以查看到全部
        QueryWrapper<Technology> wrapper = new QueryWrapper<>();
        Set<String> roles = currentUser.getRoles();
        roles.forEach(role ->{
            if(role.equals(SecurityConstant.SUPER_ROLE)){
                wrapper.eq("delldust","0");
            }else{
                wrapper.eq("delldust","0");
                wrapper.eq("responsibility_user",creator);
            }
        });
        return wrapper;
    }
    @Override
    public int techall() {
        return itechnologyMapper.selectCount(this.gainuserid());
    }

    @Override
    public int techsnum() {
        int custnum = 0;
        QueryWrapper<Technology> gainuserid = this.gainuserid();
        List<Technology> technologies = itechnologyMapper.selectList(gainuserid);
        for (Technology techno: technologies ) {
            Long id = techno.getId();
            QueryWrapper<TechnologyContributor> wrapper = new QueryWrapper<>();
            wrapper.eq("technology_id",id);
            wrapper.eq("dellpeople","0");
            Integer num = itechnologyContributorMapper.selectCount(wrapper);
            custnum+=num;
        }
        return custnum;
    }

    @Override
    public int delltech(int techid) {
        QueryWrapper<Technology> wrapper = new QueryWrapper();
        wrapper.eq("id",techid);
        Technology technology = new Technology();
        technology.setDelldust("1");
        return itechnologyMapper.update(technology,wrapper);
    }

    @Override
    public int updatetech(Technology technology) {
        QueryWrapper<Technology> wrapper = new QueryWrapper();
        wrapper.eq("id",technology.getId());
        return itechnologyMapper.update(technology,wrapper);
    }

    @Override
    public Technology viewTechnology(String id) {
        QueryWrapper<Technology> wrapper = new QueryWrapper<>();
        wrapper.eq("delldust","0");
        wrapper.eq("id",id);
        return itechnologyMapper.selectOne(wrapper);
    }
}
